
  $(document).ready(function() {   
  console.log(path1); 
    $("#add").click(function() {
      $.ajax({
        url: path1,
        type: 'GET',
        data: {
          'pno': $('#atxt').val()
        },
        success: function(data) {
          var str1 = data.productno;
          var str2 = data.productName;
          var str3 = data.price;
          var str4 = data.id;
          var flag = 0;


          /* 添加一行记录*/
          var node = $("<tr><td class = 'pno'>" + str1 + "</td><td>" + str2 + "</td><td class='pri'>" + str3 +
            "</td><td>" +
            "<input class='num digtial' type='text' name='quantity' value='1'>" +
            "<input class='digtial' type='hidden' name='prodId' value='"+str4+"'>"+
            "</td><td>" +
            "<input class='input digtial' type='text' name='price' value='"+str3+"'>"+
            "</td><td><button onclick='updtol(this)' type='button'>update</button></td><td><button onclick='delrow(this)' type='button'>delete</button></td></tr>");
          if ($('td').length == 0) {
            $('tbody').append(node);
            $('#totalprice').html(str3);
          } else {
            var tola = 0; //总金额累计
            /* 遍历每行记录*/
            $('td.pno').each(function() {
              var tol = $(this).parent().find('.input'); /*获取总价*/
              /*如果有相同记录 */
              if ($(this).text() == str1) {
                flag = 1;
                var inp = $(this).parent().find('input.num'); /*获取数量*/
                var num = parseInt(inp.val());
                inp.attr("value", num + 1); /*数量加一*/
                var pri = $(this).parent().find('td.pri'); /*获取单价*/
                var prp = parseInt(pri.text());
                tol.val(prp * (num + 1)); /*总价改变*/

              }
              var tolp = parseInt(tol.val());
              tola += tolp;
            });
            /*如果没有相同记录*/
            if (flag == 0) {
              $('tbody').append(node);
              $('#totalprice').html(tola + str3); /*总金额改变*/
            } else {
              $('#totalprice').html(tola)
            }
          }
        },
        error: function(data) {
          console.log(data);
        }
      });
    });

/*查询客户号*/
    $('#query').click(function() {
      $.ajax({
        url: path2,
        type: 'GET',
        data: {
          'by': $('#sel option:selected').val(),
          'searchText': $('#stxt').val()
        },
        success: function(data) {
          $('#customerno').html(data.customerno);
        },
        error: function(data) {
          console.log(data);
        }
      });
    });

  });
  /*删除一行记录*/
  function delrow(obj) {
      var tol = $(obj).parent().parent().find('input.input');
      var tolp = parseInt(tol.val());
      var totalprice = parseInt($('#totalprice').text());
      $('#totalprice').html(totalprice - tolp);
      $(obj).parent().parent().remove();

    }
    /*修改总价*/
  function updtol(obj) {
    var inp = $(obj).parent().parent().find('input.num');
    var num = parseInt(inp.val());
    var pri = $(obj).parent().parent().children('td.pri');
    var prp = parseInt(pri.text());
    var tol = $(obj).parent().parent().find('input.input');
    var oldtol = parseInt(tol.val());
    var totalprice = parseInt($('#totalprice').text());
    tol.val(num * prp);
    $('#totalprice').html(totalprice + num * prp - oldtol);
  }
/*提交订单和列表*/
$('#siyu').click(function(){
  $.ajax({
      url: path3,
      type: 'GET',
      data: {
        'sa.saleorderno':'${saleOrder.saleorderno}',
        'sa.customerno': $('#customerno').text(),
        'sa.total': $('#totalprice').text()
      },
      success: function(data){        
         var trs = $('tr');
          for (var i = 1; i < trs.length; i++) {
          var tds = $(trs[i]).find('.digtial');
          var idata = {};
          for (var j = 0; j < tds.length; j++) {
            idata['soi.saleId'] = data;
            idata['soi.'+tds[j].name] = tds[j].value;
            // console.log(tds[j].name,tds[j].value);
          };
          console.log(data);
          $.ajax({
            url: '@{SaleOrders.saveSaleItems}',
            type: 'GET',
            data: idata,
            success: function(data){
            },
            error: function(data){
              console.log(data);
            }
          });
        };
      },
      error: function(data){
        console.log(data);
      }
    });
  window.location.href = window.location.href;
});
